package cc.rengu.igas.bomp.common.dao.impl;

import cc.rengu.igas.bomp.common.dao.MchntSignInfoMapper;
import cc.rengu.igas.bomp.common.entity.MchntSignInfo;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;
import com.alibaba.fastjson.JSONObject;

import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by 王成 on 2020/3/2
 */
public class MchntSignInfoMapperImpl implements MchntSignInfoMapper {
    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    private String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppConfigConstant.DATABASE_CONF_NODE, AppConfigConstant.MCMP_POOL_NAME);

    @Override
    public int insertMchntSignInfo(MchntSignInfo mchntSignInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "insertMchntSignInfo");
        int iReturnCode = dbsUtil.dbsExecuteTransactionSql(sqlStr, mchntSignInfo, null);
        if (0 != iReturnCode) {
            rglog.error("登记商户签约信息失败,instId:<{}>,mchntNo:<{}>,iReturnCode:<{}>", mchntSignInfo.getInstId(), mchntSignInfo.getMchntNo(), iReturnCode);
        }
        return iReturnCode;
    }

    @Override
    public int updateMchntSignInfoById(MchntSignInfo mchntSignInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "updateMchntSignInfoById");
        int iReturnCode = dbsUtil.dbsExecuteTransactionSql(sqlStr, mchntSignInfo, null);
        if (0 != iReturnCode) {
            rglog.error("登记商户签约信息失败,instId:<{}>,mchntNo:<{}>,id:<{}>,iReturnCode:<{}>", mchntSignInfo.getInstId(), mchntSignInfo.getMchntNo(), mchntSignInfo.getId(), iReturnCode);
        }
        return iReturnCode;
    }

    @Override
    public MchntSignInfo selectMchntSignInfoById(String instId, String id) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectMchntSignInfoById");
        MchntSignInfo mchntSignInfo = new MchntSignInfo();
        mchntSignInfo.setInstId(instId);
        mchntSignInfo.setId(id);
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, mchntSignInfo, mchntSignInfo);
        if (0 != iReturnCode) {
            rglog.error("获取商户签约信息失败,instId:<{}>,id:<{}>,iReturnCode:<{}>", instId, id, iReturnCode);
            return null;
        }
        return mchntSignInfo;
    }

    @Override
    public MchntSignInfo selectMchntSignInfoByPrimaryKey(String instId, String mchntNo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectMchntSignInfoByPrimaryKey");
        MchntSignInfo mchntSignInfo = new MchntSignInfo();
        mchntSignInfo.setInstId(instId);
        mchntSignInfo.setMchntNo(mchntNo);
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, mchntSignInfo, mchntSignInfo);
        if (0 != iReturnCode) {
            rglog.error("获取商户签约信息失败,instId:<{}>,mchntNo:<{}>,iReturnCode:<{}>", instId, mchntNo, iReturnCode);
            return null;
        }
        return mchntSignInfo;
    }


    @Override
    public List<MchntSignInfo> selectBompMchntSignInfoByArtifId(String instId, String artifId, String artifIdType) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBompMchntSignInfoByArtifId");
        MchntSignInfo mchntSignInfo = new MchntSignInfo();
        mchntSignInfo.setArtifCertNo(artifId);
        mchntSignInfo.setArtifCertType(artifIdType);
        mchntSignInfo.setInstId(instId);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, mchntSignInfo, MchntSignInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("根据证件号码<{}>,证件类型类型<{}>查询签约信息失败!", artifId, artifIdType);
            return null;
        }
        return objectList.stream().map(item -> (MchntSignInfo) (item)).collect(Collectors.toList());
    }

    @Override
    public List<MchntSignInfo> selectBompMchntSignInfoByLicenceInfo(String instId, String licenceType, String licenceCode) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBompMchntSignInfoByLicenceInfo");
        MchntSignInfo mchntSignInfo = new MchntSignInfo();
        mchntSignInfo.setLicenceCode(licenceCode);
        mchntSignInfo.setLicenceType(licenceType);
        mchntSignInfo.setInstId(instId);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, mchntSignInfo, MchntSignInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("根据执照号码<{}>,执照类型类型<{}>查询签约信息为空!", licenceCode, licenceType);
            return null;
        }
        return objectList.stream().map(item -> (MchntSignInfo) (item)).collect(Collectors.toList());
    }

    @Override
    public List<MchntSignInfo> selectBompMchntSignInfoByLicenceAndCertAndMobile(String instId, String licenceType, String licenceCode, String certType, String certNo, String mobile) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBompMchntSignInfo");
        MchntSignInfo mchntSignInfo = new MchntSignInfo();
        mchntSignInfo.setInstId(instId);
        mchntSignInfo.setLicenceType(licenceType);
        mchntSignInfo.setLicenceCode(licenceCode);
        mchntSignInfo.setArtifCertType(certType);
        mchntSignInfo.setArtifCertNo(certNo);
        mchntSignInfo.setArtifMobile(mobile);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, mchntSignInfo, MchntSignInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("根据<{}>查询签约信息失败!", JSONObject.toJSONString(mchntSignInfo));
            return null;
        }
        return objectList.stream().map(item -> (MchntSignInfo) (item)).collect(Collectors.toList());
    }
}
